package com.md.youyouhui.ui.login;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.FragmentRequestPhoneBinding;
import com.md.youyouhui.net.Constants;
import com.md.youyouhui.ui.BaseFragment;
import com.md.youyouhui.ui.MainActivity;
import com.md.youyouhui.utils.SharedPreferencesUtils;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

/**
 * Created by AdministratorZJ
 * on 2019/4/3.
 */
public class RequestPhoneFragment extends BaseFragment {
    private FragmentRequestPhoneBinding phoneBinding;
    private MainViewModel viewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        phoneBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_request_phone, container, false);
        viewModel = new MainViewModel();
        initData1();
        return phoneBinding.getRoot();
    }

    private void initData1() {
        phoneBinding.requestBtn.setOnClickListener(v -> requestLogin());
        phoneBinding.requestNext.setOnClickListener(v -> {
            startActivity(new Intent(mActivity, MainActivity.class));
            if (null == getActivity()) {
                return;
            }
            getActivity().finish();
        });
    }

    private void requestLogin() {
        String phone = SharedPreferencesUtils.getString(mActivity, Constants.PHONE, "");
        String token = SharedPreferencesUtils.getString(mActivity, Constants.USER_TOKEN, "");
        String invite = phoneBinding.requestPhone.getText().toString();
        if (TextUtils.isEmpty(invite)) {
            Toast.makeText(mActivity, "请输入手机号码", Toast.LENGTH_SHORT).show();
            return;
        }
        viewModel.invitePeople( phone, invite).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    phoneBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    phoneBinding.progress.setVisibility(View.GONE);
                    startActivity(new Intent(mActivity, MainActivity.class));
                    if (null == getActivity()) {
                        return;
                    }
                    getActivity().finish();
                    break;
                case ERROR:
                    phoneBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(mActivity, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }
}
